
export default {
  components: {
  },
  props: {
    value: [Object, Array], // Map对象，K-数据ID，V-数据标题
    dataUrl: String, // 从URL地址加载数据
    queryParams: Object,
    dataFields: {
      type: Object,
      default: () => {
        return {
          key: 'key',
          title: 'title',
          value: 'key'
        }
      },
    },
    checkStrictly: {
      type: Boolean,
      default: true
    },
    isMap: {
      type: Boolean,
      default: true
    },
    isRows: { // [{id:'', name:''}, ...]
      type: Boolean,
      default: false
    },
    placeholder: String,
    maxTagCount: {
      type: Number,
      default: 2
    }
  },
  data() {
    return {
      checkedKeys: [],
      treeData: null,
      treeDataLoading: false,
      queryTreeData: window.$debounce(this._queryTreeData)
    }
  },
  computed: {
  },
  mounted() {
    this.init()
  },
  watch: {
    value() {
      this.init()
    },
    dataUrl() {
      this.init()
    },
    queryParams() {
      this.init()
    }
  },
  methods: {
    init() {
      this.initCheckedKeys()
      // this.queryTreeData()
    },
    initCheckedKeys() {
      if (this.isRows) {
        const checkedKeys = []
        if (this.value) {
          for (let row of this.value) {
            checkedKeys.push(row.id)
          }
        }
        this.checkedKeys = checkedKeys
      } else if (this.isMap) {
        const checkedKeys = []
        if (this.value) {
          for (let id in this.value) {
            checkedKeys.push(id)
          }
        }
        this.checkedKeys = checkedKeys
      } else {
        this.checkedKeys = this.value
      }
    },
    _queryTreeData() {
      this.treeDataLoading = true
      this.$http.post(this.dataUrl, this.queryParams).then((rs) => {
        const result = rs.data
        if (result.success) {
          this.treeData = result.data
        } else {
          this.treeData = null
        }
        this.treeDataLoading = false
      })
    },
    getCheckedKeys() {
      let checkedKeys = []
      if (this.checkStrictly) {
        checkedKeys = this.checkedKeys.checked
      } else {
        checkedKeys = this.checkedKeys
      }
      // console.log('doCheck: checkedKeys = %s', JSON.stringify(checkedKeys))
      return checkedKeys
    },
    doCheck() {
      // console.log('doCheck: this.checkedKeys = %s', JSON.stringify(this.checkedKeys))
      if(this.isRows) {
        const rows = []
        let checkedKeys = this.getCheckedKeys()
        for (let id of checkedKeys) {
          const row = window.$findTreeNode(this.treeData, id)
          if (row) {
            rows.push({
              id: row.id,
              name: row.name
            })
          }
        }
        // console.log('doCheck: rows = %s', JSON.stringify(rows))
        this.$emit('input', rows)
      } else if (this.isMap) {
        const map = {}
        let checkedKeys = this.getCheckedKeys()
        for (let id of checkedKeys) {
          const row = window.$findTreeNode(this.treeData, id)
          if (row) {
            map[id] = row.name
          }
        }
        // console.log('doCheck: map = %s', JSON.stringify(map))
        this.$emit('input', map)
      } else {
        let keys = this.getCheckedKeys()
        // console.log('doCheck: keys = %s', JSON.stringify(keys))
        this.$emit('input', keys)
      }
    },
    doFocus() {
      if (!this.treeData || this.treeData.length == 0) {
        this.queryTreeData()
      }
    }
  }
}